package zemberek3.crosscheck;

import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.common.io.Files;
import zemberek3.structure.Turkish;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;

/**
 * Here some lexicon helper operations are implemented. Mostly for scripting needs.
 */
public class LexiconOps {

    /**
     * Reads proper nouns from a file, adds it to target File and removes them from input.
     */
    public static void moveProperNouns(File inputToRemove, File whereToMove) throws IOException {
        LinkedHashSet<String> inputLines = Sets.newLinkedHashSet(Files.readLines(inputToRemove, Charsets.UTF_8));
        LinkedHashSet<String> outputLines = Sets.newLinkedHashSet();
        if (whereToMove.exists())
            outputLines = Sets.newLinkedHashSet(Files.readLines(whereToMove, Charsets.UTF_8));

        List<String> properNounLines = Lists.newArrayList();
        for (String line : inputLines) {
            String clean = line.trim();
            if (clean.length() == 0)
                continue;
            if (Character.isUpperCase(clean.charAt(0)))
                properNounLines.add(clean);
        }

        outputLines.addAll(properNounLines);

        List<String> sorted = Lists.newArrayList(outputLines);
        Collections.sort(sorted, Turkish.STRING_COMPARATOR_ASC);

        Files.write(Joiner.on("\n").join(sorted), whereToMove, Charsets.UTF_8);

        inputLines.removeAll(properNounLines);
        Files.write(Joiner.on("\n").join(inputLines), inputToRemove, Charsets.UTF_8);
    }

    public static void main(String[] args) throws IOException {
        moveProperNouns(
                new File("zemberek/src/main/resources/tr/master-dictionary.dict"),
                new File("zemberek/src/main/resources/tr/proper.dict"));
    }
}
